package net.xdclass.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.AddAddressRequest;
import net.xdclass.service.AddressService;
import net.xdclass.util.JsonData;
import net.xdclass.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Classname AddressController
 * @Description TODO
 * @Date 2022/4/4 17:31
 * @Created by sunhao
 */
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {

  @Autowired
  private AddressService addressService;

  @ApiOperation(value = "根据Id查询收货地址详情")
  @GetMapping("/find/{address_id}")
  public JsonData detail(@ApiParam(value = "地址Id",required = true) @PathVariable("address_id") String addressId) {
    AddressVo addressVo = addressService.detail(addressId);
    return addressVo == null? JsonData.buildResult(BizCodeEnum.ADDRESS_NOT_EXIT) : JsonData.buildSuccess(addressVo);
  }

  /**
   * 新增收货地址
   */
  @ApiOperation(value = "新增收货地址")
  @PostMapping("/add")
  public JsonData saveUserAddress(@ApiParam(value = "收货地址对象",required = true) @RequestBody AddAddressRequest addAddressRequest){
    return addressService.saveUserAddress(addAddressRequest);
  }


  /**
   * 删除指定收货地址
   */
  @ApiOperation(value = "删除指定收货地址")
  @DeleteMapping("/delete/{address_id}")
  public JsonData delete(@ApiParam(value = "地址Id",required = true) @PathVariable("address_id") String addressId){
    int rows = addressService.delete(addressId);
    return rows > 0 ? JsonData.buildSuccessMessage("删除成功"):JsonData.buildError("删除失败!");
  }


  /**
   * 查询用户的全部收货地址接口
   */
  @ApiOperation(value = "查询用户的全部收货地址接口")
  @GetMapping("/list")
  public JsonData findUserAllAddress(){
    return JsonData.buildSuccess(addressService.findUserAllAddress());
  }

}
